package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.vo.Movements;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VisitorsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/20 18:39
 * @version: 0.0.1
 * @modified By:
 */
@RequestMapping("movements")
@RestController
public class MovementsController {
    @Autowired
    private MovementsService movementsService;
    @Autowired
    private QuanziMQService quanziMQService;

    /**
     * @description 发送动态
     * @author
     * @date
     * @param
     *@return
     */
    @PostMapping
    public ResponseEntity<Void>saveMovements(@RequestParam("textContent")String textContent,
                                             @RequestParam("imageContent")MultipartFile[]imageContent,
                                             @RequestParam("location")String location,
                                             @RequestParam("longitude")String longitude,
                                             @RequestParam("latitude")String latitude){
        try {
            String publishId=this.movementsService.saveMovements(textContent,imageContent,location,longitude,latitude);
            if(StringUtils.isNotEmpty(publishId)){
                //发送消息
                this.quanziMQService.publishMsg(publishId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 查询好友动态信息
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping
    public ResponseEntity<PageResult>queryPublishList(@RequestParam(value = "page",defaultValue = "1")Integer page,@RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult=this.movementsService.queryUsePublishList(page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description  查询推荐的动态信息
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult>queryRecommendPublishList(@RequestParam(value = "page",defaultValue = "1")Integer page,@RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult=this.movementsService.queryRecommendPublishList(page,pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 点赞
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long>likeComment(@PathVariable("id")String publishId){
        try {
            Long count=this.movementsService.likeComment(publishId);
            if(null!=count){
                //发送点赞消息
                this.quanziMQService.likePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 取消点赞
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long>disLikeComment(@PathVariable("id")String publishId){
        try {
            Long count=this.movementsService.disLikeComment(publishId);
            if(null!=count){
                //发送取消点赞消息
                this.quanziMQService.disLikePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 喜欢
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long>loveComment(@PathVariable("id")String publishId){
        try {
            Long count=this.movementsService.loveComment(publishId);
            if(null!=count){
                //发送喜欢消息
                this.quanziMQService.lovePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * @description 取消喜欢
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long>unLoveComment(@PathVariable("id")String publishId){
        try {
            Long count=this.movementsService.unLoveComment(publishId);
            if(null!=count){
                //发送取消喜欢消息
                this.quanziMQService.disLovePublishMsg(publishId);
                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 查询单条动态信息
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("/{id}")
    public ResponseEntity<Movements>queryMovementsById(@PathVariable("id")String publishId){
        try {
            Movements movements= this.movementsService.queryMovementsById(publishId);
            if(null!=movements){
                //发送查询消息
                this.quanziMQService.queryPublishMsg(publishId);
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    /**
     * @description 谁看过我
     * @author
     * @date
     * @param
     *@return
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>>queryVisitorsList(){
        try {
            List<VisitorsVo>visitorsVoList=this.movementsService.queryVisitorsList();
            if(null!=visitorsVoList){
            return ResponseEntity.ok(visitorsVoList);
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @GetMapping("all")
    public ResponseEntity<PageResult>queryAlbumList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                    @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
                                                    @RequestParam(value = "userId")Long userId){
        try {
            PageResult pageResult= this.movementsService.queryAlbumList(page,pageSize,userId);
            if(null!=pageResult){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
